/**
 * Copyright 2007-2008. Chongqing First Information & Network Co., Ltd. All
 * rights reserved. <a>http://www.cqfirst.com.cn</a>
 */
package com.cqcis.uip.base.framework.handlers.adaptor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cqcis.uip.base.framework.common.State;
import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.handlers.TableDispatchService;


/**
 * @author huangmy
 * @date   2008-1-12
 */
public abstract class TableRetrieverServiceAdaptor implements TableDispatchService {
    
	public List<Task> get(String interfaceId) throws Exception {
		List<Task> tasks = new ArrayList<Task>();
		
		List<Object> objects = getObject(interfaceId);
		if (objects != null) {
			for (Iterator<Object> it = objects.iterator(); it.hasNext();) {
				Object o = it.next();
				
				Task task = new Task(interfaceId);
				task.setAttachement(o);
				task.setState(State.ERROR);
				
				tasks.add(task);
			}
			
			return tasks;
		}
				
		return null;
	}

	public boolean processResult(List<Task> results) {
		List<Object> objects = new ArrayList<Object>();
		
		if (results != null) {
			for (Iterator<Task> it = results.iterator(); it.hasNext();) {
				Task result = it.next();
				
				Object o = result.getAttachement();
				objects.add(o);
			}
			
			processObject(objects);
		}
		
		return false;
	}

	public boolean schedule(Task task) throws Exception {		
		return false;
	}
	
	/**
	 * 取数据
	 * @param interfaceId
	 * @return
	 * @throws Exception
	 */
	protected abstract List<Object> getObject(String interfaceId) throws Exception;
	
	/**
	 * 处理数据
	 * @param objects
	 */
	protected abstract void processObject(List<Object> objects);
}
